#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <map>

#define flr(x, l, r) for (int x = l; x <= r; ++x)
#define frl(x, r, l) for (int x = r; x >= l; --x)
#define fmp(x, t) for (int x = h[t]; ~x; x = ne[x])
#define LL long long
#define mt memset
#define my memcpy
#define szf sizeof
#define INF 0x3f3f3f3f
#define in(x) scanf("%d", &x)
#define out(x) printf("%d", x)
#define inll(x) scanf("%lld", &x)
#define outll(x) printf("%lld", x)
#define pn printf("\n")
#define con continue
#define bk break
#define vc vector
#define pb push_back
#define sz size
#define PII pair<int, int>
#define x first
#define y second
#define P_q priority_queue
#define ft front
#define pf push_front
#define popf pop_front
#define it insert
#define ct count

using namespace std;

const int N = 110;

int k, S, E, m, n;
int g[N][N];
int res[N][N];

void mul(int c[][N], int a[][N], int b[][N]) {
    static int temp[N][N];
    mt(temp, 0x3f, szf temp);
    
    flr (k, 1, n)
        flr (i, 1, n)
            flr (j, 1, n)
                temp[i][j] = min(temp[i][j], a[i][k] + b[k][j]);
    
    my(c, temp, szf temp);
}

void qmi() {
    mt(res, 0x3f, szf res);
    flr (i, 1, n) res[i][i] = 0;
    
    while (k) {
        if (k & 1) mul(res, res, g);
        mul(g, g, g);
        k >>= 1;
    }
}

int main() {
	in(k), in(m), in(S), in(E);
	
	mt(g, 0x3f, szf g);
	map<int, int> nums;
	nums[S] = ++ n, nums[E] = ++ n;
	S = nums[S], E = nums[E];

	while (m -- ) {
	    int a, b, c;
	    in(c), in(a), in(b);
	    if (!nums.ct(a)) nums[a] = ++ n;
	    if (!nums.ct(b)) nums[b] = ++ n;
	    a = nums[a], b = nums[b];
	    
	    g[a][b] = g[b][a] = min(g[a][b], c);
	}
    
    qmi();
    
    out(res[S][E]), pn;
    
    return 0;
}